<?php
namespace Common\Util;
/**
 * 接口返回值 服务Service
 */
class Convert {
    /*
     * 下划线转驼峰
     */
    public static function underscore2Camel($str)
    {
        $str = preg_replace_callback('/([-_]+([a-z]{1}))/i',function($matches){
            return strtoupper($matches[2]);
        },$str);
            return $str;
    }
    
    /*
     * 驼峰转下划线
     */
    public static function camel2Underscore(&$arr){
        foreach ($arr as $key=>$val) {
            if(is_array($val)) {
                self::camel2Underscore($val);
            }
            $new_key = self::from_camel_case($key);
            if($new_key == $key) {
                continue;
            }
            $arr[$new_key] = $val;
            unset($arr[$key]);
        }
        //unset($arr['lat']);
        //unset($arr['lng']);
    }
   
    public static function from_camel_case($input) {
        preg_match_all('!([A-Z][A-Z0-9]*(?=$|[A-Z][a-z0-9])|[A-Za-z][a-z0-9]+)!', $input, $matches);
        $ret = $matches[0];
        foreach ($ret as &$match) {
            $match = $match == strtoupper($match) ? strtolower($match) : lcfirst($match);
        }
        return implode('_', $ret);
    }
    
}
